package bl.controller;

import java.util.ArrayList;

import po.MemberPO;
import po.OrderPO;
import po.OrderState;

import database.factory.MemberDataFactory;
import database.factory.MemberDataFactoryImpl;
import database.factory.OrderDataFactory;
import database.factory.OrderDataFactoryImpl;
import database.service.MemberDataService;
import database.service.OrderDataService;

import vo.OrderVO;
import vo.BLToViewResultMessage;
import exception.MemberNotFoundException;
import exception.OrderCancelNotPermittedException;
import exception.OrderNotExistException;
import bl.service.OrderManagementCustomerBLService;

public class OrderManagementCustomerController implements
		OrderManagementCustomerBLService {

	@Override
	public ArrayList<OrderVO> viewMyOrder(int memberID)
			throws MemberNotFoundException {
		MemberDataFactory memberDataFactory = new MemberDataFactoryImpl();
		MemberDataService memberDataService = memberDataFactory
				.getMemberDataService();
		memberDataService.load();

		MemberPO member = memberDataService.find(memberID);
		if (member == null) {
			throw new MemberNotFoundException();
		}

		ArrayList<OrderVO> myOrderList = new ArrayList<OrderVO>();
		ArrayList<Integer> myOrderIDList = member.getOrderList();
		if (myOrderIDList == null) {
			return null;
		}
		if (myOrderIDList.size() == 0) {
			return myOrderList;
		}

		OrderDataFactory saleDataFactory = new OrderDataFactoryImpl();
		OrderDataService saleDataService = saleDataFactory.getSaleDataService();
		saleDataService.load();

		for (int i = 0; i < myOrderIDList.size(); i++) {
			int id = myOrderIDList.get(i);
			if (id <= 0) {
				continue;
			}
			OrderPO orderPO = saleDataService.find(id);
			if (orderPO == null) {
				continue;
			}
			myOrderList.add(new OrderVO(orderPO));
		}

		return myOrderList;
	}

	@Override
	public BLToViewResultMessage cancelOrder(int memberID, int orderID)
			throws MemberNotFoundException, OrderNotExistException,
			OrderCancelNotPermittedException {
		MemberDataFactory memberDataFactory = new MemberDataFactoryImpl();
		MemberDataService memberDataService = memberDataFactory
				.getMemberDataService();
		memberDataService.load();

		MemberPO member = memberDataService.find(memberID);
		if (member == null) {
			throw new MemberNotFoundException();
		}

		ArrayList<Integer> myOrderIDList = member.getOrderList();
		if (myOrderIDList == null) {
			return null;
		}

		boolean notFoundInMember = true;
		int index = -1;
		for (int i = 0; i < myOrderIDList.size(); i++) {
			int id = myOrderIDList.get(i);
			if (id != orderID) {
				continue;
			}
			notFoundInMember = false;
			index = i;
		}
		if (notFoundInMember) {
			throw new OrderNotExistException();
		}

		OrderDataFactory saleDataFactory = new OrderDataFactoryImpl();
		OrderDataService saleDataService = saleDataFactory.getSaleDataService();
		saleDataService.load();
		OrderPO order = saleDataService.find(orderID);
		if (order == null) {
			throw new OrderNotExistException();
		}
		if (order.getState() == null) {
			return null;
		}
		if (order.getState() != OrderState.PLACED) {
			throw new OrderCancelNotPermittedException();
		}
		saleDataService.delete(order);
		saleDataService.save();

		myOrderIDList.remove(index);
		memberDataService.update(member);
		memberDataService.save();

		return BLToViewResultMessage.CANCEL_ORDER_SUCCESS;
	}

	@Override
	public BLToViewResultMessage deleteOrder(int memberID, int orderID)
			throws MemberNotFoundException, OrderNotExistException {
		MemberDataFactory memberDataFactory = new MemberDataFactoryImpl();
		MemberDataService memberDataService = memberDataFactory
				.getMemberDataService();
		memberDataService.load();

		MemberPO member = memberDataService.find(memberID);
		if (member == null) {
			throw new MemberNotFoundException();
		}

		ArrayList<Integer> myOrderIDList = member.getOrderList();
		if (myOrderIDList == null) {
			return null;
		}

		boolean notFoundInMember = true;
		int index = -1;
		for (int i = 0; i < myOrderIDList.size(); i++) {
			int id = myOrderIDList.get(i);
			if (id != orderID) {
				continue;
			}
			notFoundInMember = false;
			index = i;
		}
		if (notFoundInMember) {
			throw new OrderNotExistException();
		}

		myOrderIDList.set(index, -orderID);
		memberDataService.update(member);
		memberDataService.save();

		return BLToViewResultMessage.DELETE_ORDER_SUCCESS;
	}

}
